package com.yisin.pbox.frame.base;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.yisin.pbox.frame.log.ILogger;
import com.yisin.pbox.frame.log.LogFactory;
import com.yisin.pbox.frame.util.StringUtil;

public class ActionHelper {
	static ILogger logger = LogFactory.getDefaultLogger(ActionHelper.class);

	public static Map<String, Map<String, ActionBean>> actionMap = new HashMap<String, Map<String, ActionBean>>();

	public static ActionBean excute(String uri, String queryStr,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			logger.info("request uri -> " + uri);
			String uri2 = uri.substring(1);
			String namespace = "/" + uri2.substring(0, uri2.indexOf("/"));
			uri = uri.substring(namespace.length() + 1);
			String name = "", methodName = "";
			if(uri.indexOf("!") != -1){
				String[] names = uri.split("!");
				name = names[0];
				methodName = names[1];
			} else {
				name = uri;
			}
			Map<String, ActionBean> map = actionMap.get(namespace);
			if(null == map){
				throw new ClassNotFoundException("Not Found namespace by [" + name + "] for package!");
			}
			ActionBean ab = map.get(name);
			if(null == ab){
				throw new ClassNotFoundException("Not Found Action by name [" + name + "]!");
			}
			if(StringUtil.isEmpty(methodName)){
				methodName = ab.getMethod();
			}
			logger.debug("uri -> action " + ab.getClassName());
			Class<?> actionClass = Class.forName(ab.getClassName());
			Object action = actionClass.newInstance();
			// 调用基类Action传递HttpServletRequest与HttpServletResponse
			Class<?> superClass = actionClass.getSuperclass();
			Method superMethod = superClass.getMethod("setHttpServlet", new Class[] {
					HttpServletRequest.class, HttpServletResponse.class});
			superMethod.invoke(action, new Object[] { request, response});
			
			Method method = actionClass.getMethod(methodName, new Class[] {});
			String result = StringUtil.objectToString(method.invoke(action, new Object[] {}));
			ab.setResultName(result);
			return ab;
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (NullPointerException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static void add(String name, String className, String method,
			String namespace, String drr, String xss, Map<String, String> result) {
		ActionBean ab = new ActionBean();
		ab.setName(name);
		ab.setClassName(className);
		ab.setMethod(method);
		ab.setNamespace(namespace);
		ab.setXss(xss);
		ab.setResult(result);
		if(!StringUtil.isEmpty(drr)){
			ab.setRedirect(StringUtil.parseBoolean(drr));
		}
		Map<String, ActionBean> map = new HashMap<String, ActionBean>();
		if(actionMap.get(namespace) == null){
			map.put(name, ab);
			actionMap.put(namespace, map);
		} else {
			map = actionMap.get(namespace);
		}
		map.put(name, ab);
		actionMap.put(namespace, map);
	}

}
